//
//  HBToolBarView.swift
//  sinaWeibo
//
//  Created by Beans on 16/6/26.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit

class HBToolBarView: UIView {

    var footerViewModel: HBStatusCellViewModel? {
        didSet {

//            retweetButton.setTitle("\(footerViewModel?.model?.reposts_count ?? 0)", forState: .Normal)  // 先判断==0
//            commentButton.setTitle("\(footerViewModel?.model?.comments_count ?? 0)", forState: .Normal)
//            goodButton.setTitle("\(footerViewModel?.model?.attitudes_count ?? 0)", forState: .Normal)

            repostButton.setTitle(footerViewModel?.repostCountStr, forState: .Normal)
            commentButton.setTitle(footerViewModel?.commentCountStr, forState: .Normal)
            likeButton.setTitle(footerViewModel?.attitudeCountStr, forState: .Normal)
        }
    }

    // 1. 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 2. 添加并布局子控件
    private func setupUI() {
        addSubview(repostButton)
        addSubview(commentButton)
        addSubview(likeButton)

        // 三个按钮的平铺 注意宽度
        repostButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top).offset(HomeCellMargin * 0.5)
            make.bottom.equalTo(self.snp_bottom).offset(-HomeCellMargin * 0.5)
        }
        commentButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(repostButton.snp_right)
            make.right.equalTo(likeButton.snp_left)
            make.top.bottom.equalTo(repostButton)
            // W
            make.width.equalTo(repostButton.snp_width)
        }
        likeButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(self.snp_right)
            make.top.bottom.equalTo(repostButton)
            // W
            make.width.equalTo(repostButton.snp_width)
        }
    }

    // 3. lazy load
    private lazy var repostButton: UIButton = HBFactory.getButton("转发", titleColor: .darkGrayColor(), font: 15.0, image: "timeline_icon_retweet", bgImage: "", tag: 0, target: self, method: "repostBtnClick:")

    private lazy var commentButton: UIButton = HBFactory.getButton("评论", titleColor: .darkGrayColor(), font: 15.0, image: "timeline_icon_comment", bgImage: "", tag: 0, target: self, method: "commentBtnClick:")

    private lazy var likeButton: UIButton = {
        let button = UIButton(type: .Custom)
        button.setTitle("点赞", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15.0)
        button.setImage(UIImage(named: "timeline_icon_unlike"), forState: .Normal)  // 点赞后才变为橙色的
        button.addTarget(self, action: "likeBtnClick:", forControlEvents: .TouchUpInside)
        return button
    }()

    // MARK: - 按钮的点击事件
    @objc private func repostBtnClick(btn: UIButton) {
        printLog("repost")
    }

    @objc private func commentBtnClick(btn: UIButton) {
        printLog("comment")
    }

    @objc private func likeBtnClick(btn: UIButton) {
        printLog("like")
    }
}
